package uoc.biblioteca.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import uoc.biblioteca.mappings.object.RevistaVo;

public class ReaderExcelRevistes {

	private String inputFile;
	
	//Columna 0 --> Titol
	//Columna 1 --> Autor
	//Columna 2 --> ISBN
	//Columna 3 --> EDITORIAL
	//Columna 4 --> EDICIO
	//Columna 5 --> NUMERO EJEMPLARES
	//Columna 6 --> PREU
	//Columna 7 --> URL
	//Columna 8 --> OBSERVACIONS
	
	private static int COLUMN_TITOL = 0;
	private static int COLUMN_EDITORIAL = 1;
	private static int COLUMN_ISSN = 2;
	private static int COLUMN_URL = 3;

	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public RevistaVo[] read() throws IOException  {
		
		RevistaVo[] revistes = null;
		//Llibre llibre = new Llibre();
		List revistesList = new ArrayList();
		
		File inputWorkbook = new File(inputFile);
		Workbook w;
		
		int contIdRevista=0;
		
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			
			for (int j = 0; j < sheet.getRows(); j++) {
				
				RevistaVo revista = new RevistaVo();
				
				for (int i = 0; i < sheet.getColumns(); i++) {
					
					Cell cell = sheet.getCell(i, j);

					if(j!=0){
						
						if(i==COLUMN_TITOL){
							revista.setTitol(cell.getContents());
						}
						else if(i==COLUMN_EDITORIAL){
							revista.setEditorial(cell.getContents());
						}
						else if(i==COLUMN_ISSN){
							revista.setIssn(cell.getContents());
						}
						else if(i==COLUMN_URL){
							revista.setUrl(cell.getContents());
						}
					}
				}
				if(j!=0){
					if(!("").equals(revista.getTitol())){
						revista.setIdRevista(contIdRevista);
						revistesList.add(revista);
						contIdRevista++;
					}
				}
			}
			
			revistes = (RevistaVo[]) revistesList.toArray(new RevistaVo[0]);
			
		} catch (BiffException e) {
			e.printStackTrace();
			return null;
		}
		
		return revistes;
		
	}
	
	
	public boolean validate() throws IOException  {
		
		File inputWorkbook = new File(inputFile);
		Workbook w;
		
		boolean validateError=false;
		
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			
			for (int j = 0; j < sheet.getRows(); j++) {
				
				
				for (int i = 0; i < sheet.getColumns(); i++) {
					
					Cell cell = sheet.getCell(i, j);

					if(j==0){
						if(i==COLUMN_TITOL){
							if(!cell.getContents().equals("TITOL")){
								validateError=true;
							}
						}
						if(i==COLUMN_EDITORIAL){
							if(!cell.getContents().equals("EDITORIAL")){
								validateError=true;
							}
						}
						if(i==COLUMN_ISSN){
							if(!cell.getContents().equals("ISSN")){
								validateError=true;
							}
						}
						if(i==COLUMN_EDITORIAL){
							if(!cell.getContents().equals("EDITORIAL")){
								validateError=true;
							}
						}
						if(i==COLUMN_URL){
							if(!cell.getContents().equals("URL")){
								validateError=true;
							}
						}
					}
				}
			}
			
			
		} catch (BiffException e) {
			e.printStackTrace();
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		
		return validateError;
		
	}
	
}
